import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Put,
  UseGuards,
} from '@nestjs/common';
import { ProcessTemplateService } from './process-template.service';
import { CreateProcessTemplateDto } from './dto/create-process-template.dto';
import { UpdateProcessTemplateDto } from './dto/update-process-template.dto';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { QueryPageDto } from '../../common/dto/query.page.dto';
import { QuerySizeDto } from '../size/dto/query-size.dto';
import { Prisma } from '@prisma/client';
import { QueryProcessTemplateDto } from './dto/query-process-template.dto';

@Controller('process-template')
@ApiTags('工序模板管理')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class ProcessTemplateController {
  constructor(
    private readonly processTemplateService: ProcessTemplateService,
  ) {}

  @Post()
  create(@Body() createProcessTemplateDto: CreateProcessTemplateDto) {
    return this.processTemplateService.create(createProcessTemplateDto);
  }

  @Post('page')
  findByPage(@Body() query: QueryPageDto<QueryProcessTemplateDto>) {
    const where: Prisma.processTemplateWhereInput = {
      name: {
        contains: query.model.name || undefined,
      },
    };

    return this.processTemplateService.queryPage(query, where, {
      include: {
        prices: {
          include: {
            size: {
              select: {
                name: true,
              },
            },
          },
        },
      },
    });
  }

  @Get()
  findAll() {
    return this.processTemplateService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.processTemplateService.findOne(id);
  }

  @Put(':id')
  update(
    @Param('id') id: string,
    @Body() updateProcessTemplateDto: UpdateProcessTemplateDto,
  ) {
    return this.processTemplateService.update(id, updateProcessTemplateDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.processTemplateService.remove(id);
  }
}
